#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/laser_scan.hpp>


class LidarData : public rclcpp::Node
{
public:
    LidarData()
    : Node("lidar_data_class_node")
    {
        lidar_sub_ = create_subscription<sensor_msgs::msg::LaserScan>(
            "/scan",
            10,
            std::bind(&LidarData::lidarcb, this, std::placeholders::_1)
        );
    }

private:
     void lidarcb(const sensor_msgs::msg::LaserScan::SharedPtr msg)
    {
        int nNum = msg->ranges.size();
    
        int nMid = nNum / 2;
        float fMidDist = msg->ranges[nMid];
        RCLCPP_INFO(get_logger(), "ranges[%d] = %0.3f m", nMid, fMidDist);
    }
    
    rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr lidar_sub_;
};

int main(int argc, char* argv[])
{
    rclcpp::init(argc, argv);

    auto node = std::make_shared<LidarData>();

    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}